package com.duanyf.usercenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.duanyf.usercenter.model.domain.User;
import com.duanyf.usercenter.model.domain.request.UserLoginRequest;
import com.duanyf.usercenter.model.domain.request.UserRegisterRequest;
import com.duanyf.usercenter.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.duanyf.usercenter.constant.UserConstant.ADMIN_ROLE;
import static com.duanyf.usercenter.constant.UserConstant.USER_LOGIN_STATE;

/**
 * @Description: 用户接口
 * @Author: duanyf
 * @CreateTime: 2024-05-30  23:43
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/register")
    public Long userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (Objects.isNull(userRegisterRequest)) {
            return null;
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        //controller 倾向于队请求参数的校验  service层倾向于队业务层面校验
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
            return null;
        }
        return userService.userRegister(userAccount, userPassword, checkPassword);
    }

    @PostMapping("/login")
    public User userLogin(@RequestBody UserLoginRequest loginRequest, HttpServletRequest request) {
        if (Objects.isNull(loginRequest)) {
            return null;
        }
        String userAccount = loginRequest.getUserAccount();
        String userPassword = loginRequest.getUserPassword();
        //controller 倾向于队请求参数的校验  service层倾向于队业务层面校验
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            return null;
        }
        return userService.userLogin(userAccount, userPassword, request);
    }

    @GetMapping("/search")
    public List<User> searchUsers(String userName, HttpServletRequest request) {

        //仅管理员查询
        if (!isAdmin(request)) {
            return Collections.emptyList();
        }

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(userName)) {
            userQueryWrapper.like("userName", userName);
        }
        List<User> userList = userService.list(userQueryWrapper);

        return userList.stream().map(user -> userService.getSafeUser(user)).collect(Collectors.toList());
    }

    @PostMapping("/delete")
    public boolean deleteUser(@RequestBody Long id, HttpServletRequest request) {

        //仅管理员查询
        if (!isAdmin(request)) {
            return false;
        }

        if (id <= 0) {
            return false;
        }
        return userService.removeById(id);
    }

    private boolean isAdmin(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(USER_LOGIN_STATE);
        return Objects.nonNull(user) && user.getUserRole() == ADMIN_ROLE;
    }

}
